package com.survey.demo.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.widget.layout.WrapRecyclerView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.survey.demo.R;
import com.survey.demo.app.AppActivity;
import com.survey.demo.app.AppAdapter;
import com.survey.demo.http.glide.GlideApp;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 购物车
 */
public class ShopCarActivity extends AppActivity implements OnRefreshLoadMoreListener{
    private SmartRefreshLayout mRefreshLayout;
    private WrapRecyclerView mRecyclerView;
    private TextView numTv;
    private TitleBar titleBar;
    private LinearLayout checkLayout;
    private TextView checkTv;
    private CarAdapter mAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.shop_car_activity;
    }

    @Override
    protected void initView() {
        numTv = findViewById(R.id.num_tv);
        mRefreshLayout = findViewById(R.id.rl_status_refresh);
        mRecyclerView = findViewById(R.id.car_recycle);
        mAdapter = new CarAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setOnRefreshLoadMoreListener(this);
        checkLayout = findViewById(R.id.check_layout);
        checkTv = findViewById(R.id.check_tv);
        titleBar = findViewById(R.id.top);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                if(titleBar.getRightTitle().equals("管理")){
                    titleBar.setRightTitle("完成");
                    checkTv.setText("删除");
                    checkLayout.setBackgroundColor(Color.parseColor("#d83b31"));
                }else {
                    titleBar.setRightTitle("管理");
                    checkTv.setText("结算");
                    checkLayout.setBackgroundColor(Color.parseColor("#E46006"));
                }
            }
        });
    }

    @Override
    protected void initData() {
        List<String> ss = new ArrayList<>();
        ss.add("asdas");
        ss.add("asdas");
        ss.add("asdas");
        ss.add("asdas");
        mAdapter.setData(ss);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        postDelayed(() -> {
//            mAdapter.clearData();
//            mAdapter.setData(analogData());
            mRefreshLayout.finishRefresh();
        }, 1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        postDelayed(() -> {
            //mAdapter.addData(analogData());
            mRefreshLayout.finishLoadMore();

            //mAdapter.setLastPage(mAdapter.getItemCount() >= 100);
            //mRefreshLayout.setNoMoreData(mAdapter.isLastPage());
        }, 1000);
    }
    class CarAdapter extends AppAdapter<String> {

        public CarAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder();
        }

        private final class ViewHolder extends AppAdapter<?>.ViewHolder {

            private final ImageView shop_logo;
            private final TextView shopName;
            private final RecyclerView foodRecy;

            private ViewHolder() {
                super(R.layout.car_item);
                shop_logo = findViewById(R.id.iv_logo);
                shopName = findViewById(R.id.shop_name);
                foodRecy = findViewById(R.id.food_recy);
            }

            @Override
            public void onBindView(int position) {
                GlideApp.with(ShopCarActivity.this)
                        .load("https://www.baidu.com/img/bd_logo.png")
                        .transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                20, getResources().getDisplayMetrics())))
                        .into(shop_logo);
                shopName.setText("蜜雪冰城");
                List<String> ss = new ArrayList<>();
                ss.add("asdas");
                ss.add("asdas");
                BaseQuickAdapter adapter =new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_car_shoppingcart, ss) {
                    @Override
                    protected void convert(@NotNull BaseViewHolder baseViewHolder, String bean) {
                      TextView title =  (TextView)baseViewHolder.getView(R.id.goods);
                        title.setText("海底捞海底捞海底捞海底捞海底捞海底捞");
                    }
                };
                foodRecy.setAdapter(adapter);
            }
        }
    }
}
